package cn.readjava.service.impl;

import cn.readjava.bean.Article;
import cn.readjava.dao.ArticleDao;
import cn.readjava.service.ArticleService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "article")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleDao articleDao;

    @Override
//    @Cacheable(key = "'id-'+ #p0")
    public Article getOne(long id) {
        return articleDao.getOne(id);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(long id) {
        articleDao.deleteById(id);
    }

    @Override
    @CacheEvict(allEntries = true)
    public Article save(Article article) {
        return articleDao.save(article);
    }

    @Override
    @Cacheable(key = "'ac-id-'+ #p0")
    public List<Article> findAllByArticleTypeId(long id) {
        return articleDao.findAllByArticleTypeId(id);
    }
}

